<template>
  <div class="zb-pro-table">
    <div v-show="showSearchForm" class="header">
      <SearchForm ref="form" :columns="baseFormColumns" @submit="onSubmit" />
    </div>
    <!----------底部---------------------->
    <div class="footer">
      <!-----------工具栏操作工具----------------->
      <div class="operator">
        <span style="color: #f18d22; font-size: 18px; font-weight: bold">列表</span>
        <slot name="btn"></slot>
      </div>
      <!-- ------------表格--------------->
      <div class="table">
        <el-table
          v-loading="loading"
          class="zb-table"
          :data="data"
          :border="true"
          :header-cell-style="{ background: 'rgba(230,237,249,0.7)' }"
          @selection-change="(val) => emit('selection-change', val)"
        >
          <el-table-column type="index" :index="indexMethod" />
          <template v-for="item in columns">
            <el-table-column v-if="item.slot" v-bind="{ ...item, ...{ prop: item.name } }">
              <template #default="scope">
                <slot :name="item.name" :item="item" :row="scope.row"></slot>
              </template>
            </el-table-column>
            <el-table-column v-else v-bind="{ ...item, ...{ prop: item.name } }" />
          </template>
        </el-table>
      </div>
      <!-- ------------分页--------------->
      <div class="pagination">
        <el-pagination
          v-model:current-page="pagination.pageNum"
          :page-size="pagination.pageSize"
          :page-sizes="[20, 40, 60, 100, 200, 300, 400, 1000, 2000]"
          background
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
  import SearchForm from '@/components/SearchForm/index.vue'
  import type { FormInstance } from 'element-plus'
  import { Column } from '@/utils/types'
  const ruleFormRef = ref<InstanceType<typeof FormInstance>>()
  const headerStyle = ref({ background: '#000' })
  const emit = defineEmits(['reset', 'onSubmit', 'selection-change'])
  const form = ref(null)
  let props = defineProps({
    columns: {
      type: Array as PropType<Column[]>,
      default: () => [],
    },
    data: {
      type: Array,
      default: () => [],
    },
    loading: {
      type: Boolean,
      default: false,
    },
    total: {
      type: Number,
      default: () => {
        return 0
      },
    },
    showSearchForm: {
      type: Boolean,
      default: true,
    },
  })
  const indexMethod = (index) => {
    return (pagination.pageNum - 1) * pagination.pageSize + index + 1
  }
  // 过滤调需要进行搜索选择的
  const baseFormColumns = computed(() => {
    return props.columns.filter((item) => item.valueType && item.search)
  })

  const pagination = reactive({
    pageNum: 1,
    pageSize: 20,
  })

  const pageNum = ref(1)
  // 收缩展开
  const isExpand = ref(false)
  const handleSizeChange = (val: number) => {
    pagination.pageSize = val
    emit('onSubmit', Object.assign(reactive(getData().obj), pagination))
  }
  const handleCurrentChange = (val: number) => {
    pagination.pageNum = val
    emit('onSubmit', Object.assign(reactive(getData().obj), pagination))
  }

  // const list = computed(() => {
  //   let arr = JSON.parse(JSON.stringify(props.data))
  //   return arr.splice((pagination.pageNum - 1) * 10, 10)
  // })

  const listLoading = ref(false)
  const confirmEdit = (row) => {
    row.edit = false
  }
  const cancelEdit = (row) => {
    row.edit = false
  }

  import { reactive } from 'vue'
  const getData = () => {
    let obj = {}
    let search = []
    for (let item of props.columns) {
      if (item.search) {
        obj[item.name] = item.value
      }
      if (item.search) {
        search.push(item)
      }
    }
    return { obj, search }
  }

  const formSearchData = ref(getData().search)
  const formInline = reactive(getData().obj)

  const onSubmit = (val = {}) => {
    pagination.pageNum = 1
    pagination.pageSize = 20
    console.log(form.value)

    emit('onSubmit', Object.assign(reactive(getData().obj), pagination))
  }

  const reset = () => {
    formSearchData.value.forEach((item) => {
      formInline[item.name] = null
    })
    emit('reset')
  }
  const deleteAction = (row) => {
    ElMessageBox.confirm('你确定要删除当前项吗?', '温馨提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
      draggable: true,
    })
      .then(() => {
        list.value = list.value.filter((item) => item.id !== row.id)
        ElMessage.success('删除成功')
      })
      .catch(() => {})
  }
</script>
<style scoped lang="scss">
  .headerCell {
    background-color: #252525;
  }
  .edit-input {
    padding-right: 100px;
  }
  .cancel-btn {
    position: absolute;
    right: 15px;
    top: 10px;
  }
  .zb-pro-table {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;

    .header {
      display: flex;
      padding: 16px 16px 0 16px;
      margin-bottom: 16px;
      border-radius: 4px;
      background: white;
      box-shadow: 0 0 12px rgb(0 0 0 / 5%);
      :deep(.advancedForm) {
        flex: 1;
      }
    }
    .footer {
      flex: 1;
      display: flex;
      padding: 10px;
      flex-direction: column;
      border-radius: 4px;
      overflow: hidden;
      background: white;
      box-shadow: 0 0 12px rgb(0 0 0 / 5%);
      min-height: 300px;
      .operator {
        display: flex;
        flex-direction: row;
        justify-content: space-between;
        background: rgba(31, 111, 255, 0.06);
        margin-bottom: 15px;
        align-items: center;
        padding: 10px;
      }
      .table {
        position: relative;
        flex: 1;
      }
      .zb-table {
        position: absolute;
        height: 100%;
      }
    }
    ::v-deep {
      .el-table__header th {
        font-size: 15px;
      }
    }
    .pagination {
      width: 100%;
      display: flex;
      justify-content: space-around;
      padding-top: 20px;
      box-sizing: border-box;
    }
  }
</style>
